#include "ManageUnionDB.h"
#include "LogComm.h"
#include "globe.h"
#include "servant/Application.h"
#include "SocialServant.h"
#include "ServiceDefine.h"
#include "SocialServer.h"
#include "TimeUtil.h"

//
using namespace dataproxy;

// 获取联盟列表
tars::Int32 ManageUnionList(const DBAgentServantPrx prx, std::vector<Club::UnionDBInfo> &infoList, tars::Int64 uId)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionList prx null!" << endl;
        return -1;
    }

    dbagent::TDBReadReq dataReq;
    dataReq.keyIndex = 0;
    dataReq.queryType = dbagent::E_SELECT;
    dataReq.tableName = "tb_union";

    vector<dbagent::TField> fields;
    dbagent::TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "nid";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "name";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "level";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "created";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "is_show";
    fields.push_back(tfield);

    dataReq.fields = fields;

    //where条件组
    vector<dbagent::ConditionGroup> conditionGroups;
    dbagent::ConditionGroup conditionGroup;
    conditionGroup.relation = dbagent::AND;
    vector<dbagent::Condition> conditions;
    dbagent::Condition condition;
    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::BIGINT;
    condition.colName = "uId";
    condition.colValues = L2S(uId);
    conditions.push_back(condition);

    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::BIGINT;
    condition.colName = "status";
    condition.colValues = L2S(0);
    conditions.push_back(condition);

    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);
    dataReq.conditions = conditionGroups;

    dbagent::TDBReadRsp dataRsp;
    iRet = prx->read(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionList failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    Club::UnionDBInfo info;
    for (auto it = dataRsp.records.begin(); it != dataRsp.records.end(); ++it)
    {
        for (auto itTField = it->begin(); itTField != it->end(); ++itTField)
        {
            if (itTField->colName == "nid")
            {
                info.nId = S2L(itTField->colValue);
            }
            else if (itTField->colName == "name")
            {
                info.name = itTField->colValue;
            }
            else if (itTField->colName == "level")
            {
                info.level = S2I(itTField->colValue);
            }
            else if (itTField->colName == "is_show")
            {
                info.show = S2I(itTField->colValue);
            }
            else if (itTField->colName == "created")
            {
                info.created = g_app.getOuterFactoryPtr()->GetCustomTimeTick(itTField->colValue);
            }
        }

        info.uId = uId;

        infoList.push_back(info);
    }
    __CATCH__
    FUNC_EXIT("", iRet);

    return iRet;
}

// 获取联盟DB信息
tars::Int32 ManageUnionGetDBInfo(const DBAgentServantPrx prx, Club::UnionDBInfo &info, tars::Int64 nId)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionGetDBInfo prx null!" << endl;
        return -1;
    }

    dataproxy::TReadDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_LIST) + ":" + I2S(UNION_INFO) + ":" + L2S(nId);
    dataReq.operateType = E_REDIS_READ;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_STRING;
    dataReq.clusterInfo.frageFactor = tars::hash<string>()(L2S(nId));;

    TReadDataRsp dataRsp;
    iRet = prx->redisRead(dataReq, dataRsp);
    if(iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_DEBUG << "ManageUnionGetDBInfo failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageUnionGetDBInfo ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    for (auto it = dataRsp.fields.begin(); it != dataRsp.fields.end(); ++it)
    {
        for (auto itfield = it->begin(); itfield != it->end(); ++itfield)
        {
            if (itfield->colName == "nid")
            {
                info.nId = S2L(itfield->colValue);
            }
            else if (itfield->colName == "uid")
            {
                info.uId = S2L(itfield->colValue);
            }
            else if (itfield->colName == "status")
            {
                info.status = S2I(itfield->colValue);
            }
            else if (itfield->colName == "name")
            {
                info.name = itfield->colValue;
            }
            else if (itfield->colName == "level")
            {
                info.level = S2I(itfield->colValue);
            }
            else if (itfield->colName == "is_show")
            {
                info.show = S2I(itfield->colValue);
            }
            else if (itfield->colName == "created")
            {
                info.created = g_app.getOuterFactoryPtr()->GetCustomTimeTick(itfield->colValue);
            }
        }
    }

    // 弃用的联盟代表没有
    if (info.status == 1)
    {
        return -1;
    }

    __CATCH__
    FUNC_EXIT("", iRet);

    return iRet;
}

// 获取联盟成员
tars::Int32 ManageUnionGetMember(const DBAgentServantPrx prx, Club::UnionDBMember &info, tars::Int64 nId, tars::Int64 cId)
{
    FUNC_ENTRY("");
    int iRet = 0;
     __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionGetMember prx null!" << endl;
        return -1;
    }

    dataproxy::TReadDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_LIST) + ":" + I2S(UNION_MEMBER_DEL) + ":" + L2S(cId);
    dataReq.operateType = E_REDIS_READ;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_STRING;
    dataReq.clusterInfo.frageFactor = tars::hash<string>()(L2S(cId));;

    vector<TField> fields;
    TField tfield;
    auto &conditionFields = dataReq.paraExt.fields;
    tfield.resetDefautlt();
    tfield.colValue = L2S(nId);
    conditionFields["nid"] = tfield;

    TReadDataRsp dataRsp;
    iRet = prx->redisRead(dataReq, dataRsp);
    if(iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionGetMember failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageUnionGetMember ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    for (auto it = dataRsp.fields.begin(); it != dataRsp.fields.end(); ++it)
    {
        for (auto itfield = it->begin(); itfield != it->end(); ++itfield)
        {
            if (itfield->colName == "nid")
            {
                info.nId = S2L(itfield->colValue);
            }
            else if (itfield->colName == "cid")
            {
                info.cId = S2L(itfield->colValue);
            }
            else if (itfield->colName == "joined")
            {
                info.joined = g_app.getOuterFactoryPtr()->GetCustomTimeTick(itfield->colValue);
            }
        }
    }

    __CATCH__
    FUNC_EXIT("", iRet);

    return iRet;
}

// 生成联盟ID
tars::Int32 ManageUnionGenerateID(const DBAgentServantPrx prx, tars::Int64 &nId)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    int count = 0;
    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionGenerateID prx null!" << endl;
        return -1;
    }

    do
    {
        count++;
        if (count > 100)
        {
            return -1;
        }
        nId = tars::Int64(timeutil::randomNumber(1000001, 9999999));
        iRet = ManageUnionCheckIDExist(prx, nId);
    }while(iRet != 0);

    __CATCH__
    FUNC_EXIT("", iRet);

    return iRet;
}

// 检查联盟ID唯一性
tars::Int32 ManageUnionCheckIDExist(const DBAgentServantPrx prx, tars::Int64 nId)
{
    FUNC_ENTRY("");
    int iRet = 0;
     __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionCheckIDExist prx null!" << endl;
        return -1;
    }

    dataproxy::TReadDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_HASH) + ":" + I2S(UNION_CHECK_NID) + ":" + L2S(nId);
    dataReq.operateType = E_REDIS_COUNT;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = nId;

    TReadDataRsp dataRsp;
    iRet = prx->redisRead(dataReq, dataRsp);
    if(iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionCheckIDExist failed!" << " iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    ROLLLOG_DEBUG << printTars(dataRsp) << endl;

    if (dataRsp.totalcount > 0)
    {
        return -3;
    }

    __CATCH__
    FUNC_EXIT("", iRet);

    return iRet;
}

// 检查联盟名字唯一性
tars::Int32 ManageUnionCheckNameExist(const DBAgentServantPrx prx, std::string unionName)
{
    FUNC_ENTRY("");
    int iRet = 0;
     __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionCheckNameExist prx null!" << endl;
        return -1;
    }

    dataproxy::TReadDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_HASH) + ":" + I2S(UNION_CHECK_NAME) + ":" + unionName;
    dataReq.operateType = E_REDIS_COUNT;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_STRING;
    dataReq.clusterInfo.frageFactor = tars::hash<string>()(unionName);

    TReadDataRsp dataRsp;
    iRet = prx->redisRead(dataReq, dataRsp);
    if(iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageClubCheckNameExist failed!" << " iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    ROLLLOG_DEBUG << printTars(dataRsp) << endl;

    if (dataRsp.totalcount > 0)
    {
        return -3;
    }

    __CATCH__
    FUNC_EXIT("", iRet);

    return iRet;
}

// 获取俱乐部的联盟ID
tars::Int32 ManageUnionGetIDForClub(const DBAgentServantPrx prx, tars::Int64 cId, tars::Int64 &nId)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionGetIDForClub prx null!" << endl;
        return -1;
    }

    dbagent::TDBReadReq dataReq;
    dataReq.keyIndex = 0;
    dataReq.queryType = dbagent::E_SELECT;
    dataReq.tableName = "tb_union_member";

    vector<dbagent::TField> fields;
    dbagent::TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "nid";
    fields.push_back(tfield);

    dataReq.fields = fields;

    //where条件组
    vector<dbagent::ConditionGroup> conditionGroups;
    dbagent::ConditionGroup conditionGroup;
    conditionGroup.relation = dbagent::AND;
    vector<dbagent::Condition> conditions;
    dbagent::Condition condition;
    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::BIGINT;
    condition.colName = "cid";
    condition.colValues = L2S(cId);
    conditions.push_back(condition);

    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);
    dataReq.conditions = conditionGroups;

    // 指定返回的行数的最大值
    dataReq.limit = 1;
    // 指定返回的每一行的偏移量
    dataReq.limit_from = 0;

    dbagent::TDBReadRsp dataRsp;
    iRet = prx->read(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionGetIDForClub failed!" << " iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    for (auto it = dataRsp.records.begin(); it != dataRsp.records.end(); ++it)
    {
        for (auto itField = it->begin(); itField != it->end(); ++itField)
        {
            if (itField->colName == "nid")
            {
                nId = S2L(itField->colValue);
                break;
            }
        }
    }

    __CATCH__
    FUNC_EXIT("", iRet);

    return iRet;
}

// 获取联盟成员数量
tars::Int32 ManageUnionGetMemberCount(const DBAgentServantPrx prx, tars::Int64 nId, int &count)
{
    FUNC_ENTRY("");
    int iRet = 0;
     __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionGetMemberCount prx null!" << endl;
        return -1;
    }

    dataproxy::TReadDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_HASH) + ":" + I2S(UNION_MEMBER_CID_HAS_COUNT) + ":" + I2S(nId);
    dataReq.operateType = E_REDIS_COUNT;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = nId;

    TReadDataRsp dataRsp;
    iRet = prx->redisRead(dataReq, dataRsp);
    if(iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionGetMemberCount failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageUnionGetMemberCount ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    if (dataRsp.totalcount > 0)
    {
        count = dataRsp.totalcount;
    }

    __CATCH__
    FUNC_EXIT("", iRet);

    return iRet;
}


// 获取联盟下的所有俱乐部CID
tars::Int32 ManageUnionMemberIdList(const DBAgentServantPrx prx, const long nId, vector<long> &cIdList)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (prx)
    {
        dbagent::TDBReadReq rDataReq;
        rDataReq.keyIndex = 0;
        rDataReq.queryType = dbagent::E_SELECT;
        rDataReq.tableName = "tb_union_member";

        vector<dbagent::TField> fields;
        dbagent::TField tfield;
        tfield.colArithType = E_NONE;
        tfield.colName = "cid";
        fields.push_back(tfield);

        rDataReq.fields = fields;

        //where条件组
        vector<dbagent::ConditionGroup> conditionGroups;
        dbagent::ConditionGroup conditionGroup;
        conditionGroup.relation = dbagent::AND;
        vector<dbagent::Condition> conditions;
        dbagent::Condition condition;
        condition.condtion = dbagent::E_EQ;
        condition.colType = dbagent::BIGINT;
        condition.colName = "nid";
        condition.colValues = L2S(nId);
        conditions.push_back(condition);
    
        conditionGroup.condition = conditions;
        conditionGroups.push_back(conditionGroup);
        rDataReq.conditions = conditionGroups;

        dbagent::TDBReadRsp rDataRsp;
        iRet = prx->read(rDataReq, rDataRsp);
        if (iRet != 0 || rDataRsp.iResult != 0)
        {
            ROLLLOG_ERROR << "read data from dbagent failed, rDataReq: " << printTars(rDataReq) << ", rDataRsp: " << printTars(rDataRsp) << endl;
            return -1;
        }

        for (auto it = rDataRsp.records.begin(); it != rDataRsp.records.end(); ++it)
        {
            for (auto itField = it->begin(); itField != it->end(); ++itField)
            {
                if (itField->colName == "cid")
                {
                    cIdList.push_back(S2L(itField->colValue));
                    break;
                }
            }
        }

        return iRet;
    }

    __CATCH__
    FUNC_EXIT("", iRet);

    return -1;
}


/*********************insert******************/
//TODO 创建联盟
tars::Int32 ManageUnionInsert(const DBAgentServantPrx prx, tars::Int64 nId, tars::Int64 uId, std::string name, tars::Int32 lv)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionInsert prx null!" << endl;
        return -1;
    }

    dataproxy::TWriteDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_HASH) + ":" + I2S(UNION_INFO) + ":" + L2S(nId);
    dataReq.operateType = E_REDIS_INSERT;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = nId;

    vector<TField> fields;
    TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "nid";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(nId);
    fields.push_back(tfield);

    tfield.colName = "name";
    tfield.colType = STRING;
    tfield.colValue = name;
    fields.push_back(tfield);

    tfield.colName = "level";
    tfield.colType = INT;
    tfield.colValue = I2S(lv);
    fields.push_back(tfield);

    tfield.colName = "uid";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(uId);
    fields.push_back(tfield);

    tfield.colName = "is_show";
    tfield.colType = INT;
    tfield.colValue = I2S(1);
    fields.push_back(tfield);

    tfield.colName = "status";
    tfield.colType = INT;
    tfield.colValue = I2S(0);
    fields.push_back(tfield);

    tfield.colName = "created";
    tfield.colType = STRING;
    tfield.colValue = g_app.getOuterFactoryPtr()->GetTLogTimeFormat();
    fields.push_back(tfield);

    dataReq.fields = fields;

    dataproxy::TWriteDataRsp dataRsp;
    iRet = prx->redisWrite(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionInsert failed!" << " iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageUnionInsert ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

// 创建联盟成员
tars::Int32 ManageUnionInsertMember(const DBAgentServantPrx prx, tars::Int64 nId, tars::Int64 cId)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionInsertMember prx null!" << endl;
        return -1;
    }

    dataproxy::TWriteDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_LIST) + ":" + I2S(UNION_MEMBER_CREATE) + ":" + L2S(nId);
    dataReq.operateType = E_REDIS_INSERT;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = nId;

    vector<TField> fields;
    TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "nid";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(nId);
    fields.push_back(tfield);

    tfield.colName = "cid";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(cId);
    fields.push_back(tfield);
    
    tfield.colName = "position";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(1);
    fields.push_back(tfield);

    tfield.colName = "joined";
    tfield.colType = STRING;
    tfield.colValue = g_app.getOuterFactoryPtr()->GetTLogTimeFormat();
    fields.push_back(tfield);

    dataReq.fields = fields;

    dataproxy::TWriteDataRsp dataRsp;
    iRet = prx->redisWrite(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionInsertMember failed!" << " iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageUnionInsertMember ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

// 更新联盟信息
tars::Int32 ManageUnionModify(const DBAgentServantPrx prx, tars::Int64 nId, int flag, int status, std::string name, int level, int show)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionModify prx null!" << endl;
        return -1;
    }

    dataproxy::TWriteDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_HASH) + ":" + I2S(UNION_INFO_UPDATE) + ":" + L2S(nId);
    dataReq.operateType = E_REDIS_WRITE;
    dataReq.paraExt.resetDefautlt();
    dataReq.paraExt.queryType = dbagent::E_UPDATE;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = nId;

    vector<TField> fields;
    TField tfield;
    tfield.colArithType = E_NONE;
    if (flag == 1)
    {
        tfield.colName = "name";
        tfield.colType = STRING;
        tfield.colValue = name;
        fields.push_back(tfield);
    }
    else if (flag == 2)
    {
        tfield.colName = "level";
        tfield.colType = INT;
        tfield.colValue = I2S(level);
        fields.push_back(tfield);
    }
    else if (flag == 3)
    {
        tfield.colName = "is_show";
        tfield.colType = INT;
        tfield.colValue = I2S(show);
        fields.push_back(tfield);
    }
    else
    {
        tfield.colName = "status";
        tfield.colType = INT;
        tfield.colValue = I2S(status);
        fields.push_back(tfield);
    }

    dataReq.fields = fields;

    dataproxy::TWriteDataRsp dataRsp;
    iRet = prx->redisWrite(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionModify failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageUnionModify ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

// 删除联盟成员
tars::Int32 ManageUnionDeleteMember(const DBAgentServantPrx prx, tars::Int64 nId, tars::Int64 cId)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionDeleteMember prx null!" << endl;
        return -1;
    }

    dataproxy::TWriteDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_LIST) + ":" + I2S(UNION_MEMBER_DEL) + ":" + L2S(cId);
    dataReq.operateType = E_REDIS_WRITE;
    dataReq.paraExt.resetDefautlt();
    dataReq.paraExt.queryType = dbagent::E_DELETE;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = cId;

    vector<TField> fields;
    TField tfield;
    auto &conditionFields = dataReq.paraExt.fields;
    tfield.resetDefautlt();
    tfield.colValue = L2S(nId);
    conditionFields["nid"] = tfield;
   
    dataproxy::TWriteDataRsp dataRsp;
    iRet = prx->redisWrite(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionDeleteMember failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageUnionDeleteMember ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

// 申请列表
tars::Int32 ManageUnionApplyList(const DBAgentServantPrx prx, std::vector<Club::UnionApplyInfo> &infoList, int &count, string &sNid, tars::Int32 pageIndex)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionApplyList prx null!" << endl;
        return -1;
    }

    dbagent::TDBReadReq dataReq;
    dataReq.keyIndex = 0;
    dataReq.queryType = dbagent::E_SELECT;
    dataReq.tableName = "tb_union_apply";

    vector<dbagent::TField> fields;
    dbagent::TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "id";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "nid";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "cid";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "msg";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "applied";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "status";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "answer";
    fields.push_back(tfield);

    dataReq.fields = fields;

    //where条件组
    vector<dbagent::ConditionGroup> conditionGroups;
    dbagent::ConditionGroup conditionGroup;
    conditionGroup.relation = dbagent::AND;
    vector<dbagent::Condition> conditions;
    dbagent::Condition condition;
    condition.condtion = dbagent::E_IN;
    condition.colType = dbagent::BIGINT;
    condition.colName = "nid";
    condition.colValues = sNid;
    conditions.push_back(condition);
    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);
    dataReq.conditions = conditionGroups;

    // orderby
    vector<dbagent::OrderBy> orderBys;
    dbagent::OrderBy orderBy;
    orderBy.sort = dbagent::ASC;
    orderBy.colName = "status";
    orderBys.push_back(orderBy);
    orderBy.sort = dbagent::DESC;
    orderBy.colName = "applied";
    orderBys.push_back(orderBy);

    dataReq.orderbyCol = orderBys;

    // 指定返回的行数的最大值
    dataReq.limit = 50;
    // 指定返回的每一行的偏移量
    dataReq.limit_from = pageIndex > 0? (pageIndex - 1)*50 : 0;;

    dbagent::TDBReadRsp dataRsp;
    iRet = prx->read(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionApplyList failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageUnionApplyList ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    Club::UnionApplyInfo info;
    for (auto it = dataRsp.records.begin(); it != dataRsp.records.end(); ++it)
    {
        for (auto itTField = it->begin(); itTField != it->end(); ++itTField)
        {
            if (itTField->colName == "id")
            {
                info.id = S2L(itTField->colValue);
            }
            else if (itTField->colName == "nid")
            {
                info.nId = S2L(itTField->colValue);
            }
            else if (itTField->colName == "cid")
            {
                info.cId = S2L(itTField->colValue);
            }
            else if (itTField->colName == "msg")
            {
                info.msg = itTField->colValue;
            }
            else if (itTField->colName == "status")
            {
                info.status = S2I(itTField->colValue);
            }
            else if (itTField->colName == "answer")
            {
                info.uId = S2L(itTField->colValue);
            }
            else if (itTField->colName == "applied")
            {
                info.time = g_app.getOuterFactoryPtr()->GetCustomTimeTick(itTField->colValue);
            }
        }

        infoList.push_back(info);
    }

    count = dataRsp.totalcount;


    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

// 获取联盟单个申请详情
tars::Int32 ManageUnionGetApply(const DBAgentServantPrx prx, Club::UnionApplyInfo &info, tars::Int64 id)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionGetApply prx null!" << endl;
        return -1;
    }

    dbagent::TDBReadReq dataReq;
    dataReq.keyIndex = 0;
    dataReq.queryType = dbagent::E_SELECT;
    dataReq.tableName = "tb_union_apply";

    vector<dbagent::TField> fields;
    dbagent::TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "nid";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "cid";
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "status";
    fields.push_back(tfield);

    dataReq.fields = fields;

    //where条件组
    vector<dbagent::ConditionGroup> conditionGroups;
    dbagent::ConditionGroup conditionGroup;
    conditionGroup.relation = dbagent::AND;
    vector<dbagent::Condition> conditions;
    dbagent::Condition condition;
    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::BIGINT;
    condition.colName = "id";
    condition.colValues = L2S(id);
    conditions.push_back(condition);
    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);
    dataReq.conditions = conditionGroups;

    // 指定返回的行数的最大值
    dataReq.limit = 1;
    // 指定返回的每一行的偏移量
    dataReq.limit_from = 0;

    dbagent::TDBReadRsp dataRsp;
    iRet = prx->read(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionGetApply failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -1;
    }

    ROLLLOG_DEBUG << "ManageUnionGetApply ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    for (auto it = dataRsp.records.begin(); it != dataRsp.records.end(); ++it)
    {
        for (auto itTField = it->begin(); itTField != it->end(); ++itTField)
        {
            if (itTField->colName == "nid")
            {
                info.nId = S2L(itTField->colValue);
            }
            else if (itTField->colName == "cid")
            {
                info.cId = S2I(itTField->colValue);
            }
            else if (itTField->colName == "status")
            {
                info.status = S2I(itTField->colValue);
            }
        }
    }

    info.id = id;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

// 创建联盟申请
tars::Int32 ManageUnionInsertApply(const DBAgentServantPrx prx, tars::Int64 nId, tars::Int64 cId, tars::Int32 status, std::string msg)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionInsertApply prx null!" << endl;
        return -1;
    }

    dataproxy::TWriteDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_LIST) + ":" + I2S(UNION_APPLY_CREATE) + ":" + L2S(cId);
    dataReq.operateType = E_REDIS_INSERT;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_STRING;
    dataReq.clusterInfo.frageFactor = tars::hash<string>()(L2S(nId));

    vector<TField> fields;
    TField tfield;
    tfield.colArithType = E_NONE;
    tfield.colName = "nid";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(nId);
    fields.push_back(tfield);

    tfield.colName = "cid";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(cId);
    fields.push_back(tfield);

    tfield.colName = "answer";
    tfield.colType = BIGINT;
    tfield.colValue = L2S(0);
    fields.push_back(tfield);

    tfield.colName = "status";
    tfield.colType = INT;
    tfield.colValue = I2S(status);
    fields.push_back(tfield);

    tfield.colName = "msg";
    tfield.colType = STRING;
    tfield.colValue = msg;
    fields.push_back(tfield);

    tfield.colName = "applied";
    tfield.colType = STRING;
    tfield.colValue = g_app.getOuterFactoryPtr()->GetTLogTimeFormat();
    fields.push_back(tfield);

    dataReq.fields = fields;

    dataproxy::TWriteDataRsp dataRsp;
    iRet = prx->redisWrite(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionInsertApply failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageUnionInsertApply ok! dataReq:" << printTars(dataReq) << endl;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

// 检测联盟申请信息
bool ManageUnionCheckApply(const DBAgentServantPrx prx, tars::Int64 nId, tars::Int64 cId)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionCheckApply prx null!" << endl;
        return false;
    }

    dataproxy::TReadDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_HASH) + ":" + I2S(UNION_APPLY_UPDATE) + ":" + L2S(nId);
    dataReq.operateType = E_REDIS_COUNT;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = nId;

    vector<TField> fields;
    dbagent::TField tfield;
    auto &conditionFields = dataReq.paraExt.fields;
    tfield.resetDefautlt();
    tfield.colValue = L2S(cId);
    conditionFields["cid"] = tfield;
    tfield.colValue = I2S(0);
    conditionFields["status"] = tfield;

    TReadDataRsp dataRsp;
    iRet = prx->redisRead(dataReq, dataRsp);
    if(iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionCheckApply failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return false;
    }

    ROLLLOG_DEBUG << "ManageUnionCheckApply ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;
    if (dataRsp.totalcount > 0)
    {
        return true;
    }
  
    __CATCH__
    FUNC_EXIT("", iRet);

    return false;
}

// 更新联盟申请
tars::Int32 ManageUnionUpdateApply(const DBAgentServantPrx prx, tars::Int64 nId, tars::Int64 cId, std::string msg)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionUpdateApply prx null!" << endl;
        return -1;
    }

    dataproxy::TWriteDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_LIST) + ":" + I2S(UNION_APPLY_UPDATE) + ":" + L2S(nId);
    dataReq.operateType = E_REDIS_WRITE;
    dataReq.paraExt.resetDefautlt();
    dataReq.paraExt.queryType = dbagent::E_UPDATE;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = nId;

    vector<TField> fields;
    dbagent::TField tfield;
    tfield.resetDefautlt();
    tfield.colArithType = E_NONE;
    tfield.colName = "msg";
    tfield.colType = STRING;
    tfield.colValue = msg;
    fields.push_back(tfield);

    tfield.colArithType = E_NONE;
    tfield.colName = "applied";
    tfield.colType = STRING;
    tfield.colValue = g_app.getOuterFactoryPtr()->GetTLogTimeFormat();
    fields.push_back(tfield);

    dataReq.fields = fields;

    auto &conditionFields = dataReq.paraExt.fields;
    tfield.resetDefautlt();
    tfield.colValue = L2S(cId);
    conditionFields["cid"] = tfield;
    tfield.colValue = I2S(0);
    conditionFields["status"] = tfield;

    dataproxy::TWriteDataRsp dataRsp;
    iRet = prx->redisWrite(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionUpdateApply failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageUnionUpdateApply ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}

// 修改联盟申请信息
tars::Int32 ManageUnionModifyApply(const DBAgentServantPrx prx, tars::Int64 id, tars::Int64 uId, tars::Int32 status)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageUnionModifyApply prx null!" << endl;
        return -1;
    }

    dbagent::TDBWriteReq dataReq;
    dataReq.keyIndex = 0;
    dataReq.queryType = dbagent::E_UPDATE;
    dataReq.tableName = "tb_union_apply";

    vector<dbagent::TField> fields;
    dbagent::TField tfield;
    tfield.colArithType = E_NONE;

    tfield.colName = "answer";
    tfield.colType = dbagent::BIGINT;
    tfield.colValue = L2S(uId);
    fields.push_back(tfield);

    tfield.colName = "status";
    tfield.colType = dbagent::BIGINT;
    tfield.colValue = I2S(status);
    fields.push_back(tfield);

    dataReq.fields = fields;

    //where条件组
    vector<dbagent::ConditionGroup> conditionGroups;
    dbagent::ConditionGroup conditionGroup;
    conditionGroup.relation = dbagent::AND;
    vector<dbagent::Condition> conditions;
    dbagent::Condition condition;
    condition.condtion = dbagent::E_EQ;
    condition.colType = dbagent::BIGINT;
    condition.colName = "id";
    condition.colValues = L2S(id);
    conditions.push_back(condition);
    conditionGroup.condition = conditions;
    conditionGroups.push_back(conditionGroup);
    dataReq.conditions = conditionGroups;

    dbagent::TDBWriteRsp dataRsp;
    iRet = prx->write(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageUnionModifyApply failed!  iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << ", dataRsp: " << printTars(dataRsp) << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageUnionModifyApply ok! dataReq:" << printTars(dataReq) << ", dataRsp:" << printTars(dataRsp) << endl;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}